# 导入模块
import requests
# 导入lxml模块解析HTML
from lxml import etree

douban_movie_base_url = 'https://movie.douban.com/top250?start={}&filter='
headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"
}
# 将数据写入文件保存
with open("data/movie_info.csv", mode="w", encoding='utf8') as f:
    for i in range(1, 11):
        douban_movie_url = douban_movie_base_url.format((i - 1) * 25)
        # 使用requests模块通过get方式发起请求
        response = requests.get(url=douban_movie_url, headers=headers)
        print("当前正在获取：", douban_movie_url)
        # # 获取返回的文本类数据
        # print(response.text)
        # 使用xpath解析html页面
        # 参考地址：https://zhuanlan.zhihu.com/p/29436838
        xpath_obj = etree.HTML(response.text, etree.HTMLParser())
        # 搜索整个HTML页面，找到其中属性值为grid_view的ol标签下的所有li标签
        for li in xpath_obj.xpath("//ol[@class='grid_view']/li"):
            # 解析每一部影片的信息
            item = li.xpath("./div[@class='item']/div[@class='info']")[0]
            title = item.xpath("./div[1]/a/span[1]/text()")[0]
            rating = item.xpath(".//span[@class='rating_num']/text()")[0]
            quote_list = item.xpath(".//p[@class='quote']/span/text()")
            quote = ""
            if len(quote_list) == 1:
                quote = quote_list[0]
            # print(title, rating, quote)
            f.write(title + "," + rating + "," + quote + "\n")

            # 解析每部影片的封面图片
            pic = li.xpath("./div[@class='item']/div[@class='pic']")[0]
            # 提取影片的排名
            rank = pic.xpath("./em/text()")[0]
            # 提取每部影片的封面链接
            pic_url = pic.xpath("./a/img/@src")[0]
            # 再次发起请求 获取封面图片
            pic_res = requests.get(url=pic_url, headers=headers)
            print("正在获取%s影片的封面" % title)
            # 获取返回的二进制内容，并通过二进制写入的方式保存到文件中
            with open("data/pic/%s-%s.jpg" % (rank, title), mode="wb") as pic_f:
                pic_f.write(pic_res.content)
